
## Load the data

## In particular, load gay, str, all, and friendship matrix into environment
## Also load 'P' dataframe of all people into the environment.
## WARNING: This script makes global variables!



### read in Java-made files  (matrix.csv and allFolks.csv made by RegParser2)
load.data = function( db = "mydb", event="macaw1",
		INCLUDE_ALL = c("NotNo", "Pending", "In","Paid","Test","All"),
		collapse.groups=TRUE ) {
							
	# Set up the database
	library( "RSQLite" )
	m <- dbDriver("SQLite")
	cat( "LOADING DATA FROM", db, "\n" )
   con <- dbConnect(m, dbname = db)
	print( dbListTables(con) )
		
## Make a matrix of two-way matches out of the one-way match matrix
bidirectional.clean = function( M, avg = FALSE) {
	# match links if they are bi-directional
	if ( avg ) {
		M = (M + t(M)) / 2
	} else {
		M = pmin( M, t(M) )
	}
	diag(M) = 0
	
	M
}


add.hand.links = function( addLinks, mat, no.adding=FALSE, 
						  no.warning=FALSE ) {
  
  warnings = c()
  addMiss = function( warnings, whoA, whoB ) {
  	paste( warnings, paste( whoA, whoB, sep=" or " ), "; " )
  }
  broke = 0
  ## Add links from the addl list
  if ( !is.null( addLinks ) ) {
    for ( i in 1:nrow(addLinks) ) {
      tp = addLinks[i,]
      if ( !( tp[[1]] %in% rownames(mat) ) || !( tp[[2]] %in% rownames(mat) ) ) {
        if ( !no.warning ) {
          warnings = addMiss( warnings,  tp[[1]], tp[[2]] )
          cat( "Missing: ", tp[[1]], " or ", tp[[2]], "\n" )
        }
      } else {
        if ( tp[[3]] == "y" ) {
          if ( !no.adding ) {
			 cat( "Adding date possiblity ", tp[[1]], tp[[2]], "\n" )
          	 mat[[ tp[[1]], tp[[2]] ]] = mat[[ tp[[2]], tp[[1]] ]] = 1
          	 
          }
        } else {
          ##cat( "Breaking ", tp[[1]], tp[[2]], "\n" )       
          if ( mat[[ tp[[1]], tp[[2]] ]] != 0 && mat[[ tp[[2]], tp[[1]] ]] != 0 ) {
          	broke = broke + 1
          }
          mat[[ tp[[1]], tp[[2]] ]] = mat[[ tp[[2]], tp[[1]] ]] = 0
        }
      }
    }
  }
  cat( "Added/Broke ", broke, " links out of ", nrow(addLinks), "\n" )
 
  if ( !no.warning && length( warnings ) > 0 ) {
  	warning( warnings )
  }
   
  ## return the updated matrix
  mat
}


loadAdditionalLinks = function( forWho ) {

	if (  "additionals" %in% dbListTables(con) ) {

	forWho = paste( forWho, collapse="', '" )
	
	qry = sprintf( "SELECT MatchA, MatchB, Include, WHY from additionals WHERE MatchA IN ('%s') AND MatchB IN ('%s')", forWho, forWho )
	rs = dbSendQuery( con, qry )
	dat <- fetch(rs, n = -1)  
	
	dat
	} else {
		warning( "NO ADDITIONALS TABLE" )
		c()
	}
}


write.reg.table = function( ) {

		load.data( "All" )

		P$name1 = paste( P$first1, P$last1 )
		P$name2 = paste( P$first2, P$last2 )
		P$name3 = paste( P$first3, P$last3 )
			nonEmpty = function( X ) {
					!is.na(X) & (X != "") & (X != 0) & (X != " ") & (X != " NA")
			}
		P$name = ifelse( nonEmpty(P$name2), paste( P$name1, P$name2, sep=" & " ), P$name1 )
		P$name = ifelse( nonEmpty(P$name3), paste( P$name, P$name3, sep=" & " ), P$name )
	
	
		TP = P[ c("personID","name" ) ]
		ban = read.csv( "banList.csv", stringsAsFactors=FALSE )
		banT = ban[ c("personID","pubname","Paid","Beer") ]
		TP = merge( TP, banT )
		TP = TP[ c(2,1,3,4,5) ]
		TP = TP[ order(toupper(TP$name)), ]

		write.csv( TP, file="regFile.csv" )
}

							
							
  
  INCLUDE_ALL = match.arg(INCLUDE_ALL)
  cat( "Inclusion mode = ", INCLUDE_ALL, "\n" )

  P = read.csv( "allFolks.csv", stringsAsFactors=FALSE );
  rownames(P) = P$personID

 
  ## Read in list of things to break
  ## previous dates, old_dates.csv -- machine-generated prev dates extra links file
  addLinks = loadAdditionalLinks( rownames(P) )
  
  
  ## add hand-tailored links (and breaks) ##
  gay.mat = as.matrix( read.csv( "gay-matrix.csv", row.names=1) )
  str.mat = as.matrix( read.csv( "str-matrix.csv", row.names=1) )

  gay.mat = add.hand.links( addLinks, gay.mat )
  str.mat = add.hand.links( addLinks, str.mat, no.warning=TRUE )
  cat( "Added hand links from additional.csv and old_dates.csv (if existing).\n" )

  select <- switch( INCLUDE_ALL,
      "NotNo" = "AND cancelled=0",
    	"Paid" = "AND paid=1",
    	"Pending" = "AND (pending=1 OR paid=1) AND cancelled=0",
  	   "In" = "AND here=1",
  	   "Test" = "LIMIT 20",
  	   "All" = {
  	   	   warning( "INCLUDING ALL PEOPLE -- IGNORING PAY AND IN LIST\n" )
  	   	   "" 
  	   	} )
   
   qry = sprintf( "SELECT psdid FROM register_regrecord WHERE event='%s' %s", 
   					event, select )
	print(qry)
   rs = dbSendQuery( con, qry )
   pids = fetch(rs, n=-1)$psdid
   cat( "Selected", length(pids), "records\n" )
	nms = rownames(gay.mat)
	going = nms %in% pids

   stopifnot( all( rownames(gay.mat) == colnames(gay.mat) ) )
   stopifnot( all( rownames(P) == rownames(gay.mat) ) )
   stopifnot( all( rownames(str.mat) == rownames(gay.mat) ) )
   stopifnot( all( colnames(str.mat) == colnames(gay.mat) ) )
   
   gay.mat = gay.mat[ going, going ]
   str.mat = str.mat[ going, going ]
   P = P[ going, ]
 
  ## Make nametag and final match lookup file ##

  nt = P[ c("group","first1","last1","first2","last2", "first3","last3", "personID", "pubname", "email") ]
  nt2 = nt[ P$group == "yes", ]  #make dup nametags for groups
  nt2$first1 = nt2$first2
  nt2$last1 = nt2$last2
  nt = rbind(nt, nt2 )
  nt3 = nt[ P$first3 != "", ]
  nt3$first1 = nt3$first3
  nt3$last1 = nt3$last3
  nt = rbind( nt, nt3 )
  nt$first2 = nt$last2= nt$first3 = nt$last3 = NULL
  nt$last1 = substr(nt$last1,0,1)
  nt = nt[ order(toupper(nt$pubname), nt$personID), ]
  write.csv( nt, file="nametags.csv", row.names=FALSE,quote=FALSE )
  cat( "Name tag file written\n" )


  ## turn matrix into two-way links only
  all.mat = gay.mat | str.mat
  friend.mat = !( all.mat )

  friend.mat = add.hand.links( addLinks, friend.mat, no.adding=TRUE, no.warning=TRUE )
  friend.mat[ P$friendship != "yes", ] = 0

  P$oneway = apply( all.mat, 1, sum )
  gay.mat = bidirectional.clean( gay.mat, avg=FALSE )
  str.mat = bidirectional.clean( str.mat, avg=FALSE )
  all.mat = bidirectional.clean( all.mat, avg=FALSE )
  friend.mat = bidirectional.clean( friend.mat, avg=FALSE )

  ## count number of matches people have
  P$matches = apply( all.mat > 0, 1, sum )

  ## and who!
  P$who.gay = apply( gay.mat, 2, function(x) { 
  	paste( rownames(gay.mat)[x > 0], collapse=" ", sep=" " ) } )
  P$who.str = apply( str.mat, 2, function(x) { 
  	paste( rownames(str.mat)[x > 0], collapse=" ", sep=" " ) } )

                                        ## sort for ease of reading
  # P2 = P[ order( P$group, P$isMan1, P$age1, P$matches ), ]
  #  save.file( P, collapse.groups )
  rownames(P) = P$personID

  gay.mat <<- gay.mat
  str.mat <<- str.mat
  all.mat <<- all.mat
  friend.mat <<- friend.mat
  P <<- P

  "exported all variables to global environment - hack"
}



save.file = function( P, collapse.groups=FALSE, file="allCounts.csv",
				orderBy=c("group","isMan1","matches") ) {
	
    P2 =  P[ do.call( order, P[orderBy] ), ]

                                        ## names(P)[29:48]
                                        ## strip off triads and quads--none in this database.
  #warning( "Removing quads (if any) from allCounts.csv" )
  killCols = c(  pmatch("remarks",names(P2)), #pmatch("email",names(P2)),
  							pmatch("friends",names(P2)),
  							pmatch("referred",names(P2)),
  							pmatch("addList",names(P2)) )
  killCols = killCols[!is.na(killCols)]
  
  if ( collapse.groups ) {
  		
	killCols = c( killCols, grep( "[234]$", names(P2) ),
  						grep("from",names(P2) ) )
  	nm1 = grep( "1$", names(P2) )
	nm2 = grep( "2$", names(P2) )
	nm3 = grep( "3$", names(P2) )
	nonEmpty = function( X ) {
		!is.na(X) & (X != "") & (X != 0)
	}

	for ( i in 1:length(nm1) ) {
		n1 = nm1[i]
		n2 = nm2[i]
		n3 = nm3[i]
		P2[n1] = ifelse( nonEmpty(P2[[n2]]), paste( P2[[n1]], P2[[n2]], sep="/" ), P2[[n1]] )
		P2[n1] = ifelse( nonEmpty(P2[[n3]]), paste( P2[[n1]], P2[[n3]], sep="/" ), P2[[n1]] )
	}
	
	P2$group = ifelse( nonEmpty(P2$group), paste( P2$group, P2$groupStyle, sep="-" ), P2$group )
	# remove specific genders and look genders
	n = as.numeric( unlist( sapply( c( "groupStyle","isMan", "isWoman", "isTransM", "isTransW", "isGQ","lookM", "lookW", "lookTransM", "lookTransW", "lookGQ", "lookNone" ), grep, names(P2), simplify=FALSE ) ) )
	killCols = c( killCols, n )
  }
							
  write.csv( P2[-killCols], file=file, row.names=FALSE,quote=FALSE )
}



## END of Data Cleaning and Organizing ##


